clear			all
set 			more off
cd 				"~/Dropbox/Indonesia Migration/" // Specify your root directory here

/*******************************************************************************
This file is to be executed after constructing the IV. 
It constructs the outcome variables. 
********************************************************************************/
foreach			t in 1976 1980 1985 1990 1995 2000 2005 2010 {
use				"Data/Census_`t'.dta", clear
keep			geolev2 geolev1 year migtype oldmig bpl classwk sex edattain ///
				indgen ind empstat perwt native
bys				year geolev2: egen pop_cst = sum(perwt)		
bys				year geolev2: egen total_native = sum(native*perwt)
gen				LFP = 1 if inrange(empstat,1,2)
replace			LFP = 0 if missing(LFP)	& !missing(empstat)
bys				geolev2 year: egen total_LFP = sum(LFP*perwt)
bys				geolev2 year: gen prop_LFP = total_LFP/pop_cst
gen				unemp = 1 if empstat == 2
replace			unemp = 0 if missing(unemp) & !missing(empstat)
bys				geolev2 year: egen total_unemp = sum(unemp*perwt)
bys				geolev2 year: gen prop_unemp = total_unemp/pop_cst
bys				geolev2 year: gen prop_unemp_2 = total_unemp/total_LFP
gen				emp = 1 if inrange(classwk,1,4)
replace			emp = 0 if missing(emp)	& !missing(classwk)
bys				geolev2 year: egen total_emp = sum(emp*perwt)
bys				geolev2 year: gen prop_emp = total_emp/pop_cst
gen				wage = 1 if classwk == 2
replace			wage = 0 if missing(wage) & !missing(classwk)
bys				geolev2 year: egen total_wage = sum(wage*perwt) 
bys				geolev2 year: gen prop_wage = total_wage/pop_cst
gen				SE = 1 if classwk == 1
replace			SE = 0 if missing(SE) & !missing(classwk)
bys				geolev2 year: egen total_SE = sum(SE*perwt)
bys				year geolev2: gen prop_SE = total_SE/pop_cst
gen				unpaid = 1 if classwk == 3
replace			unpaid = 0 if missing(unpaid) & !missing(classwk)
bys				geolev2 year: egen total_unpaid = sum(unpaid*perwt)
bys				year geolev2: gen prop_unpaid = total_unpaid/pop_cst
drop			classwk empstat

*CONTROL VARIABLES
generate		high_educ = 1 if inrange(edattain,3,4)
replace			high_educ = 0 if inrange(edattain,1,2)
generate		male = 1 if sex == 1
replace			male = 0 if inrange(sex,2,9)
gen				male_LFP = 1 if LFP == 1 & male == 1
bys				year geolev2: egen total_male_LFP = total(male_LFP*perwt)
bys				year geolev2: gen prop_male_LFP = total_male_LFP/pop_cst
rename 			prop_male_LFP male_LFP_per
bys				year geolev2: egen prop_male_LFP = max(male_LFP_per)
gen				female_LFP = 1 if LFP == 1 & male == 0
bys				year geolev2: egen total_female_LFP = total(female_LFP*perwt)
bys				year geolev2: gen prop_female_LFP = total_female_LFP/pop_cst
rename 			prop_female_LFP female_LFP_per
bys				year geolev2: egen prop_female_LFP = max(female_LFP_per)
gen				male_emp = 1 if emp == 1 & male == 1
bys				year geolev2: egen total_male_emp = total(male_emp*perwt)
bys				year geolev2: gen prop_male_emp = total_male_emp/pop_cst
rename 			prop_male_emp male_emp_per
bys				year geolev2: egen prop_male_emp = max(male_emp_per*perwt)
gen				female_emp = 1 if emp == 1 & male == 0
bys				year geolev2: egen total_female_emp = total(female_emp*perwt)
bys				year geolev2: gen prop_female_emp = total_female_emp/pop_cst
rename 			prop_female_emp female_emp_per
bys				year geolev2: egen prop_female_emp = max(female_emp_per)
gen				male_wage = 1 if wage == 1 & male == 1
bys				year geolev2: egen total_male_wage = total(male_wage*perwt)
bys				year geolev2: gen prop_male_wage = total_male_wage/pop_cst
rename 			prop_male_wage male_wage_per
bys				year geolev2: egen prop_male_wage = max(male_wage_per)
gen				female_wage = 1 if wage == 1 & male == 0
bys				year geolev2: egen total_female_wage = total(female_wage*perwt)
bys				year geolev2: gen prop_female_wage = total_female_wage/pop_cst
rename 			prop_female_wage female_wage_per
bys				year geolev2: egen prop_female_wage = max(female_wage_per)
gen				male_SE = 1 if SE == 1 & male == 1
bys				year geolev2: egen total_male_SE = total(male_SE*perwt)
bys				year geolev2: gen prop_male_SE = total_male_SE/pop_cst
rename 			prop_male_SE male_SE_per
bys				year geolev2: egen prop_male_SE = max(male_SE_per)
keep			prop_* high_educ male LFP emp wage SE unpaid unemp pop_cst native ///
				total_native  migtype bpl ///
				geolev2 geolev1 year oldmig perwt edattain			
gen				female_SE = 1 if SE == 1 & male == 0
bys				year geolev2: egen total_female_SE = total(female_SE*perwt)
bys				year geolev2: gen prop_female_SE = total_female_SE/pop_cst
rename 			prop_female_SE female_SE_per
bys				year geolev2: egen prop_female_SE = max(female_SE_per)
gen				male_unpaid = 1 if unpaid == 1 & male == 1
bys				year geolev2: egen total_male_unpaid = total(male_unpaid*perwt)
bys				year geolev2: gen prop_male_unpaid = total_male_unpaid/pop_cst
rename 			prop_male_unpaid male_unpaid_per
bys				year geolev2: egen prop_male_unpaid = max(male_unpaid_per)
gen				female_unpaid = 1 if unpaid == 1 & male == 0
bys				year geolev2: egen total_female_unpaid = total(female_unpaid*perwt)
bys				year geolev2: gen prop_female_unpaid = total_female_unpaid/pop_cst
rename 			prop_female_unpaid female_unpaid_per
bys				year geolev2: egen prop_female_unpaid = max(female_unpaid_per)
gen				male_unemp = 1 if unemp == 1 & male == 1
bys				year geolev2: egen total_male_unemp = total(male_unemp*perwt)
bys				year geolev2: gen prop_male_unemp = total_male_unemp/pop_cst
rename 			prop_male_unemp male_unemp_per
bys				year geolev2: egen prop_male_unemp = max(male_unemp_per)
gen				female_unemp = 1 if unemp == 1 & male == 0
bys				year geolev2: egen total_female_unemp = total(female_unemp*perwt)
bys				year geolev2: gen prop_female_unemp = total_female_unemp/pop_cst
rename 			prop_female_unemp female_unemp_per
bys				year geolev2: egen prop_female_unemp = max(female_unemp_per)
keep			prop_* high_educ male LFP emp wage SE unpaid unemp pop_cst native ///
				total_native  migtype bpl ///
				geolev2 geolev1 year oldmig perwt edattain
***********************************************************************************************
gen				high_educ_LFP = 1 if LFP == 1 & high_educ == 1
bys				year geolev2: egen total_high_educ_LFP = total(high_educ_LFP*perwt)
bys				year geolev2: gen prop_high_educ_LFP = total_high_educ_LFP/pop_cst
rename 			prop_high_educ_LFP high_educ_LFP_per
bys				year geolev2: egen prop_high_educ_LFP = max(high_educ_LFP_per)
gen				low_educ_LFP = 1 if LFP == 1 & high_educ == 0
bys				year geolev2: egen total_low_educ_LFPp = total(low_educ_LFP*perwt)
bys				year geolev2: gen prop_low_educ_LFP = total_low_educ_LFP/pop_cst
rename 			prop_low_educ_LFP low_educ_LFP_per
bys				year geolev2: egen prop_low_educ_LFP = max(low_educ_LFP_per)
gen				high_educ_emp = 1 if emp == 1 & high_educ == 1
bys				year geolev2: egen total_high_educ_emp = total(high_educ_emp*perwt)
bys				year geolev2: gen prop_high_educ_emp = total_high_educ_emp/pop_cst
rename 			prop_high_educ_emp high_educ_emp_per
bys				year geolev2: egen prop_high_educ_emp = max(high_educ_emp_per)
gen				low_educ_emp = 1 if emp == 1 & high_educ == 0
bys				year geolev2: egen total_low_educ_empp = total(low_educ_emp*perwt)
bys				year geolev2: gen prop_low_educ_emp = total_low_educ_emp/pop_cst
rename 			prop_low_educ_emp low_educ_emp_per
bys				year geolev2: egen prop_low_educ_emp = max(low_educ_emp_per)
gen				high_educ_wage = 1 if wage == 1 & high_educ == 1
bys				year geolev2: egen total_high_educ_wage = total(high_educ_wage*perwt)
bys				year geolev2: gen prop_high_educ_wage = total_high_educ_wage/pop_cst
rename 			prop_high_educ_wage high_educ_wage_per
bys				year geolev2: egen prop_high_educ_wage = max(high_educ_wage_per)
keep			prop_* high_educ male LFP emp wage SE unpaid unemp pop_cst native ///
				total_native  migtype bpl ///
				geolev2 geolev1 year oldmig perwt edattain
						
gen				low_educ_wage = 1 if wage == 1 & high_educ == 0
bys				year geolev2: egen total_low_educ_wagep = total(low_educ_wage*perwt)
bys				year geolev2: gen prop_low_educ_wage = total_low_educ_wage/pop_cst
rename 			prop_low_educ_wage low_educ_wage_per
bys				year geolev2: egen prop_low_educ_wage = max(low_educ_wage_per)
gen				high_educ_SE = 1 if SE == 1 & high_educ == 1
bys				year geolev2: egen total_high_educ_SE = total(high_educ_SE*perwt)
bys				year geolev2: gen prop_high_educ_SE = total_high_educ_SE/pop_cst
rename 			prop_high_educ_SE high_educ_SE_per
bys				year geolev2: egen prop_high_educ_SE = max(high_educ_SE_per)
gen				low_educ_SE = 1 if SE == 1 & high_educ == 0
bys				year geolev2: egen total_low_educ_SEp = total(low_educ_SE*perwt)
bys				year geolev2: gen prop_low_educ_SE = total_low_educ_SE/pop_cst
rename 			prop_low_educ_SE low_educ_SE_per
bys				year geolev2: egen prop_low_educ_SE = max(low_educ_SE_per)
gen				high_educ_unpaid = 1 if unpaid == 1 & high_educ == 1
bys				year geolev2: egen total_high_educ_unpaid = total(high_educ_unpaid*perwt)
bys				year geolev2: gen prop_high_educ_unpaid = total_high_educ_unpaid/pop_cst
rename 			prop_high_educ_unpaid high_educ_unpaid_per
bys				year geolev2: egen prop_high_educ_unpaid = max(high_educ_unpaid_per)
gen				low_educ_unpaid = 1 if unpaid == 1 & high_educ == 0
bys				year geolev2: egen total_low_educ_unpaidp = total(low_educ_unpaid*perwt)
bys				year geolev2: gen prop_low_educ_unpaid = total_low_educ_unpaid/pop_cst
rename 			prop_low_educ_unpaid low_educ_unpaid_per
bys				year geolev2: egen prop_low_educ_unpaid = max(low_educ_unpaid_per)
gen				high_educ_unemp = 1 if unemp == 1 & high_educ == 1
bys				year geolev2: egen total_high_educ_unemp = total(high_educ_unemp*perwt)
bys				year geolev2: gen prop_high_educ_unemp = total_high_educ_unemp/pop_cst
rename 			prop_high_educ_unemp high_educ_unemp_per
bys				year geolev2: egen prop_high_educ_unemp = max(high_educ_unemp_per)
gen				low_educ_unemp = 1 if unemp == 1 & high_educ == 0
bys				year geolev2: egen total_low_educ_unempp = total(low_educ_unemp*perwt)
bys				year geolev2: gen prop_low_educ_unemp = total_low_educ_unemp/pop_cst
rename 			prop_low_educ_unemp low_educ_unemp_per
bys				year geolev2: egen prop_low_educ_unemp = max(low_educ_unemp_per)
keep			prop_* high_educ male LFP emp wage SE unpaid unemp pop_cst native ///
				total_native  migtype bpl ///
				geolev2 geolev1 year oldmig perwt edattain				
*********************************************************************************************
gen				native_emp = 1 if emp == 1 & native == 1
bys				year geolev2: egen total_native_emp = total(native_emp*perwt)
bys				year geolev2: gen prop_native_emp = total_native_emp/pop_cst
rename 			prop_native_emp native_emp_per
bys				year geolev2: egen prop_native_emp = max(native_emp_per)
gen				mig_emp_n = 1 if emp == 1 & migtype == 1
bys				year geolev2: egen total_mig_emp_n = total(mig_emp_n*perwt)
bys				year geolev2: gen prop_mig_emp_n = total_mig_emp_n/pop_cst
rename 			prop_mig_emp_n mig_emp_per_n
bys				year geolev2: egen prop_mig_emp_n = max(mig_emp_per_n)
gen				mig_emp_o = 1 if emp == 1 & oldmig == 1
bys				year geolev2: egen total_mig_emp_o = total(mig_emp_o*perwt)
bys				year geolev2: gen prop_mig_emp_o = total_mig_emp_o/pop_cst
rename 			prop_mig_emp_o mig_emp_per_o
bys				year geolev2: egen prop_mig_emp_o = max(mig_emp_per_o)
keep			prop_* high_educ male LFP emp wage SE unpaid unemp pop_cst native ///
				total_native  migtype bpl ///
				geolev2 geolev1 year oldmig perwt edattain
*********************************************************************************************
gen				native_LFP = 1 if LFP == 1 & native == 1
bys				year geolev2: egen total_native_LFP = total(native_LFP*perwt)
bys				year geolev2: gen prop_native_LFP = total_native_LFP/pop_cst
rename 			prop_native_LFP native_LFP_per
bys				year geolev2: egen prop_native_LFP = max(native_LFP_per)
gen				mig_LFP_n = 1 if LFP == 1 & migtype == 1
bys				year geolev2: egen total_mig_LFP_n = total(mig_LFP_n*perwt)
bys				year geolev2: gen prop_mig_LFP_n = total_mig_LFP_n/pop_cst
rename 			prop_mig_LFP_n mig_LFP_per_n
bys				year geolev2: egen prop_mig_LFP_n = max(mig_LFP_per_n)
gen				mig_LFP_o = 1 if LFP == 1 & oldmig == 1
bys				year geolev2: egen total_mig_LFP_o = total(mig_LFP_o*perwt)
bys				year geolev2: gen prop_mig_LFP_o = total_mig_LFP_o/pop_cst
rename 			prop_mig_LFP_o mig_LFP_per_o
bys				year geolev2: egen prop_mig_LFP_o = max(mig_LFP_per_o)
keep			prop_* high_educ male LFP emp wage SE unpaid unemp pop_cst native ///
				total_native  migtype bpl ///
				geolev2 geolev1 year oldmig perwt edattain
*******************************************************************************************
gen				native_SE = 1 if SE == 1 & native == 1
bys				year geolev2: egen total_native_SE = total(native_SE*perwt)
bys				year geolev2: gen prop_native_SE = total_native_SE/pop_cst
rename 			prop_native_SE native_SE_per
bys				year geolev2: egen prop_native_SE = max(native_SE_per)
gen				mig_SE_n = 1 if SE == 1 & migtype == 1
bys				year geolev2: egen total_mig_SE_n = total(mig_SE_n*perwt)
bys				year geolev2: gen prop_mig_SE_n = total_mig_SE_n/pop_cst
rename 			prop_mig_SE_n mig_SE_per_n
bys				year geolev2: egen prop_mig_SE_n = max(mig_SE_per_n)
gen				mig_SE_o = 1 if SE == 1 & oldmig == 1
bys				year geolev2: egen total_mig_SE_o = total(mig_SE_o*perwt)
bys				year geolev2: gen prop_mig_SE_o = total_mig_SE_o/pop_cst
rename 			prop_mig_SE_o mig_SE_per_o
bys				year geolev2: egen prop_mig_SE_o = max(mig_SE_per_o)
keep			prop_* high_educ male LFP emp wage SE unpaid unemp pop_cst native ///
				total_native  migtype bpl ///
				geolev2 geolev1 year oldmig perwt edattain
**********************************************************************************************
gen				native_wage = 1 if wage == 1 & native == 1
bys				year geolev2: egen total_native_wage = total(native_wage*perwt)
bys				year geolev2: gen prop_native_wage = total_native_wage/pop_cst
rename 			prop_native_wage native_wage_per
bys				year geolev2: egen prop_native_wage = max(native_wage_per)
gen				mig_wage_n = 1 if wage == 1 & migtype == 1
bys				year geolev2: egen total_mig_wage_n = total(mig_wage_n*perwt) 
bys				year geolev2: gen prop_mig_wage_n = total_mig_wage_n/pop_cst
rename 			prop_mig_wage_n mig_wage_per_n
bys				year geolev2: egen prop_mig_wage_n = max(mig_wage_per_n)
gen				mig_wage_o = 1 if wage == 1 & oldmig == 1
bys				year geolev2: egen total_mig_wage_o = total(mig_wage_o*perwt) 
bys				year geolev2: gen prop_mig_wage_o = total_mig_wage_o/pop_cst
rename 			prop_mig_wage_o mig_wage_per_o
bys				year geolev2: egen prop_mig_wage_o = max(mig_wage_per_o)
keep			prop_* high_educ male LFP emp wage SE unpaid unemp pop_cst native ///
				total_native  migtype bpl ///
				geolev2 geolev1 year oldmig perwt edattain
********************************************************************************************
gen				native_unpaid = 1 if unpaid == 1 & native == 1
bys				year geolev2: egen total_native_unpaid = total(native_unpaid*perwt)
bys				year geolev2: gen prop_native_unpaid = total_native_unpaid/pop_cst
rename 			prop_native_unpaid native_unpaid_per
bys				year geolev2: egen prop_native_unpaid = max(native_unpaid_per)
gen				mig_unpaid_n = 1 if unpaid == 1 & migtype == 1
bys				year geolev2: egen total_mig_unpaid_n = total(mig_unpaid_n*perwt)
bys				year geolev2: gen prop_mig_unpaid_n = total_mig_unpaid_n/pop_cst
rename 			prop_mig_unpaid_n mig_unpaid_per_n
bys				year geolev2: egen prop_mig_unpaid_n = max(mig_unpaid_per_n)
gen				mig_unpaid_o = 1 if unpaid == 1 & oldmig == 1
bys				year geolev2: egen total_mig_unpaid_o = total(mig_unpaid_o*perwt)
bys				year geolev2: gen prop_mig_unpaid_o = total_mig_unpaid_o/pop_cst
rename 			prop_mig_unpaid_o mig_unpaid_per_o
bys				year geolev2: egen prop_mig_unpaid_o = max(mig_unpaid_per_o)
keep			prop_* high_educ male LFP emp wage SE unpaid unemp pop_cst native ///
				total_native  migtype bpl ///
				geolev2 geolev1 year oldmig perwt edattain
********************************************************************************************
gen				native_unemp = 1 if unemp == 1 & native == 1
bys				year geolev2: egen total_native_unemp = total(native_unemp*perwt)
bys				year geolev2: gen prop_native_unemp = total_native_unemp/pop_cst
rename 			prop_native_unemp native_unemp_per
bys				year geolev2: egen prop_native_unemp = max(native_unemp_per)
gen				mig_unemp_n = 1 if unemp == 1 & migtype == 1
bys				year geolev2: egen total_mig_unemp_n = total(mig_unemp_n*perwt) 
bys				year geolev2: gen prop_mig_unemp_n = total_mig_unemp_n/pop_cst
rename 			prop_mig_unemp_n mig_unemp_per_n
bys				year geolev2: egen prop_mig_unemp_n = max(mig_unemp_per_n)
gen				mig_unemp_o = 1 if unemp == 1 & oldmig == 1
bys				year geolev2: egen total_mig_unemp_o = total(mig_unemp_o*perwt) 
bys				year geolev2: gen prop_mig_unemp_o = total_mig_unemp_o/pop_cst
rename 			prop_mig_unemp_o mig_unemp_per_o
bys				year geolev2: egen prop_mig_unemp_o = max(mig_unemp_per_o)

*Among Everyone
gen				low_educ_wage_native = 1 if wage == 1 & high_educ == 0 & native == 1
bys				year geolev2: egen total_low_educ_wage_native = total(low_educ_wage_native*perwt)
bys				year geolev2: gen prop_low_educ_wage_native = total_low_educ_wage_native/pop_cst
rename 			prop_low_educ_wage_native low_educ_wage_per_native
bys				year geolev2: egen prop_low_educ_wage_native = max(low_educ_wage_per_native)
gen				low_educ_wage_mig_o = 1 if wage == 1 & high_educ == 0 & oldmig == 1
bys				year geolev2: egen total_low_educ_wage_mig_o = total(low_educ_wage_mig_o*perwt)
bys				year geolev2: gen prop_low_educ_wage_mig_o = total_low_educ_wage_mig_o/pop_cst
rename 			prop_low_educ_wage_mig_o low_educ_wage_per_mig_o
bys				year geolev2: egen prop_low_educ_wage_mig_o = max(low_educ_wage_per_mig_o)
gen				high_educ_wage_native = 1 if wage == 1 & high_educ == 1 & native == 1
bys				year geolev2: egen total_high_educ_wage_native = total(high_educ_wage_native*perwt)
bys				year geolev2: gen prop_high_educ_wage_native = total_high_educ_wage_native/pop_cst
rename 			prop_high_educ_wage_native high_educ_wage_per_native
bys				year geolev2: egen prop_high_educ_wage_native = max(high_educ_wage_per_native)
gen				high_educ_wage_mig_o = 1 if wage == 1 & high_educ == 1 & oldmig == 1
bys				year geolev2: egen total_high_educ_wage_mig_o = total(high_educ_wage_mig_o*perwt)
bys				year geolev2: gen prop_high_educ_wage_mig_o = total_high_educ_wage_mig_o/pop_cst
rename 			prop_high_educ_wage_mig_o high_educ_wage_per_mig_o
bys				year geolev2: egen prop_high_educ_wage_mig_o = max(high_educ_wage_per_mig_o)
gen				high_educ_wage_mig_n = 1 if wage == 1 & high_educ == 1 & migtype == 1
bys				year geolev2: egen total_high_educ_wage_mig_n = total(high_educ_wage_mig_n*perwt)
bys				year geolev2: gen prop_high_educ_wage_mig_n = total_high_educ_wage_mig_n/pop_cst
rename 			prop_high_educ_wage_mig_n high_educ_wage_per_mig_n
bys				year geolev2: egen prop_high_educ_wage_mig_n = max(high_educ_wage_per_mig_n)
gen				low_educ_wage_mig_n = 1 if wage == 1 & high_educ == 0 & migtype == 1
bys				year geolev2: egen total_low_educ_wage_mig_n = total(low_educ_wage_mig_n*perwt)
bys				year geolev2: gen prop_low_educ_wage_mig_n = total_low_educ_wage_mig_n/pop_cst
rename 			prop_low_educ_wage_mig_n low_educ_wage_per_mig_n
bys				year geolev2: egen prop_low_educ_wage_mig_n = max(low_educ_wage_per_mig_n)


gen				ed_1_wage_native = 1 if wage == 1 & edattain == 1 & native == 1
bys				year geolev2: egen total_ed_1_wage_native = total(ed_1_wage_native*perwt)
bys				year geolev2: gen prop_ed_1_wage_native = total_ed_1_wage_native/pop_cst
rename 			prop_ed_1_wage_native ed_1_wage_per_native
bys				year geolev2: egen prop_ed_1_wage_native = max(ed_1_wage_per_native)
gen				ed_2_wage_native = 1 if wage == 1 & edattain == 2 & native == 1
bys				year geolev2: egen total_ed_2_wage_native = total(ed_2_wage_native*perwt)
bys				year geolev2: gen prop_ed_2_wage_native = total_ed_2_wage_native/pop_cst
rename 			prop_ed_2_wage_native ed_2_wage_per_native
bys				year geolev2: egen prop_ed_2_wage_native = max(ed_2_wage_per_native)
gen				ed_3_wage_native = 1 if wage == 1 & edattain == 3 & native == 1
bys				year geolev2: egen total_ed_3_wage_native = total(ed_3_wage_native*perwt)
bys				year geolev2: gen prop_ed_3_wage_native = total_ed_3_wage_native/pop_cst
rename 			prop_ed_3_wage_native ed_3_wage_per_native
bys				year geolev2: egen prop_ed_3_wage_native = max(ed_3_wage_per_native)
gen				ed_4_wage_native = 1 if wage == 1 & edattain == 4 & native == 1
bys				year geolev2: egen total_ed_4_wage_native = total(ed_4_wage_native*perwt)
bys				year geolev2: gen prop_ed_4_wage_native = total_ed_4_wage_native/pop_cst
rename 			prop_ed_4_wage_native ed_4_wage_per_native
bys				year geolev2: egen prop_ed_4_wage_native = max(ed_4_wage_per_native)
gen				ed_1_SE_native = 1 if SE == 1 & edattain == 1 & native == 1
bys				year geolev2: egen total_ed_1_SE_native = total(ed_1_SE_native*perwt)
bys				year geolev2: gen prop_ed_1_SE_native = total_ed_1_SE_native/pop_cst
rename 			prop_ed_1_SE_native ed_1_SE_per_native
bys				year geolev2: egen prop_ed_1_SE_native = max(ed_1_SE_per_native)
gen				ed_2_SE_native = 1 if SE == 1 & edattain == 2 & native == 1
bys				year geolev2: egen total_ed_2_SE_native = total(ed_2_SE_native*perwt)
bys				year geolev2: gen prop_ed_2_SE_native = total_ed_2_SE_native/pop_cst
rename 			prop_ed_2_SE_native ed_2_SE_per_native
bys				year geolev2: egen prop_ed_2_SE_native = max(ed_2_SE_per_native)
gen				ed_3_SE_native = 1 if SE == 1 & edattain == 3 & native == 1
bys				year geolev2: egen total_ed_3_SE_native = total(ed_3_SE_native*perwt)
bys				year geolev2: gen prop_ed_3_SE_native = total_ed_3_SE_native/pop_cst
rename 			prop_ed_3_SE_native ed_3_SE_per_native
bys				year geolev2: egen prop_ed_3_SE_native = max(ed_3_SE_per_native)
gen				ed_4_SE_native = 1 if SE == 1 & edattain == 4 & native == 1
bys				year geolev2: egen total_ed_4_SE_native = total(ed_4_SE_native*perwt)
bys				year geolev2: gen prop_ed_4_SE_native = total_ed_4_SE_native/pop_cst
rename 			prop_ed_4_SE_native ed_4_SE_per_native
bys				year geolev2: egen prop_ed_4_SE_native = max(ed_4_SE_per_native)

gen				ed_1_unpaid_native = 1 if unpaid == 1 & edattain == 1 & native == 1
bys				year geolev2: egen total_ed_1_unpaid_native = total(ed_1_unpaid_native*perwt)
bys				year geolev2: gen prop_ed_1_unpaid_native = total_ed_1_unpaid_native/pop_cst
rename 			prop_ed_1_unpaid_native ed_1_unpaid_per_native
bys				year geolev2: egen prop_ed_1_unpaid_native = max(ed_1_unpaid_per_native)
gen				ed_2_unpaid_native = 1 if unpaid == 1 & edattain == 2 & native == 1
bys				year geolev2: egen total_ed_2_unpaid_native = total(ed_2_unpaid_native*perwt)
bys				year geolev2: gen prop_ed_2_unpaid_native = total_ed_2_unpaid_native/pop_cst
rename 			prop_ed_2_unpaid_native ed_2_unpaid_per_native
bys				year geolev2: egen prop_ed_2_unpaid_native = max(ed_2_unpaid_per_native)
gen				ed_3_unpaid_native = 1 if unpaid == 1 & edattain == 3 & native == 1
bys				year geolev2: egen total_ed_3_unpaid_native = total(ed_3_unpaid_native*perwt)
bys				year geolev2: gen prop_ed_3_unpaid_native = total_ed_3_unpaid_native/pop_cst
rename 			prop_ed_3_unpaid_native ed_3_unpaid_per_native
bys				year geolev2: egen prop_ed_3_unpaid_native = max(ed_3_unpaid_per_native)
gen				ed_4_unpaid_native = 1 if unpaid == 1 & edattain == 4 & native == 1
bys				year geolev2: egen total_ed_4_unpaid_native = total(ed_4_unpaid_native*perwt)
bys				year geolev2: gen prop_ed_4_unpaid_native = total_ed_4_unpaid_native/pop_cst
rename 			prop_ed_4_unpaid_native ed_4_unpaid_per_native
bys				year geolev2: egen prop_ed_4_unpaid_native = max(ed_4_unpaid_per_native)

gen				ed_1_emp_native = 1 if emp == 1 & edattain == 1 & native == 1
bys				year geolev2: egen total_ed_1_emp_native = total(ed_1_emp_native*perwt)
bys				year geolev2: gen prop_ed_1_emp_native = total_ed_1_emp_native/pop_cst
rename 			prop_ed_1_emp_native ed_1_emp_per_native
bys				year geolev2: egen prop_ed_1_emp_native = max(ed_1_emp_per_native)
gen				ed_2_emp_native = 1 if emp == 1 & edattain == 2 & native == 1
bys				year geolev2: egen total_ed_2_emp_native = total(ed_2_emp_native*perwt)
bys				year geolev2: gen prop_ed_2_emp_native = total_ed_2_emp_native/pop_cst
rename 			prop_ed_2_emp_native ed_2_emp_per_native
bys				year geolev2: egen prop_ed_2_emp_native = max(ed_2_emp_per_native)
gen				ed_3_emp_native = 1 if emp == 1 & edattain == 3 & native == 1
bys				year geolev2: egen total_ed_3_emp_native = total(ed_3_emp_native*perwt)
bys				year geolev2: gen prop_ed_3_emp_native = total_ed_3_emp_native/pop_cst
rename 			prop_ed_3_emp_native ed_3_emp_per_native
bys				year geolev2: egen prop_ed_3_emp_native = max(ed_3_emp_per_native)
gen				ed_4_emp_native = 1 if emp == 1 & edattain == 4 & native == 1
bys				year geolev2: egen total_ed_4_emp_native = total(ed_4_emp_native*perwt)
bys				year geolev2: gen prop_ed_4_emp_native = total_ed_4_emp_native/pop_cst
rename 			prop_ed_4_emp_native ed_4_emp_per_native
bys				year geolev2: egen prop_ed_4_emp_native = max(ed_4_emp_per_native)
keep			prop* geolev2 geolev1 year LFP emp wage SE emp unemp unpaid high_educ native oldmig migtype pop_cst ///
				total_native perwt edattain

gen				low_educ_SE_native = 1 if SE == 1 & high_educ == 0 & native == 1
bys				year geolev2: egen total_low_educ_SE_native = total(low_educ_SE_native*perwt)
bys				year geolev2: gen prop_low_educ_SE_native = total_low_educ_SE_native/pop_cst
rename 			prop_low_educ_SE_native low_educ_SE_per_native
bys				year geolev2: egen prop_low_educ_SE_native = max(low_educ_SE_per_native)
gen				low_educ_SE_mig_o = 1 if SE == 1 & high_educ == 0 & oldmig == 1
bys				year geolev2: egen total_low_educ_SE_mig_o = total(low_educ_SE_mig_o*perwt)
bys				year geolev2: gen prop_low_educ_SE_mig_o = total_low_educ_SE_mig_o/pop_cst
rename 			prop_low_educ_SE_mig_o low_educ_SE_per_mig_o
bys				year geolev2: egen prop_low_educ_SE_mig_o = max(low_educ_SE_per_mig_o)
keep			prop* geolev2 geolev1 year LFP emp wage SE emp unemp unpaid high_educ native oldmig migtype pop_cst ///
				total_native  perwt edattain

gen				high_educ_SE_native = 1 if SE == 1 & high_educ == 1 & native == 1
bys				year geolev2: egen total_high_educ_SE_native = total(high_educ_SE_native*perwt)
bys				year geolev2: gen prop_high_educ_SE_native = total_high_educ_SE_native/pop_cst
rename 			prop_high_educ_SE_native high_educ_SE_per_native
bys				year geolev2: egen prop_high_educ_SE_native = max(high_educ_SE_per_native)

gen				high_educ_SE_mig_o = 1 if SE == 1 & high_educ == 1 & oldmig == 1
bys				year geolev2: egen total_high_educ_SE_mig_o = total(high_educ_SE_mig_o*perwt)
bys				year geolev2: gen prop_high_educ_SE_mig_o = total_high_educ_SE_mig_o/pop_cst
rename 			prop_high_educ_SE_mig_o high_educ_SE_per_mig_o
bys				year geolev2: egen prop_high_educ_SE_mig_o = max(high_educ_SE_per_mig_o)
gen				high_educ_SE_mig_n = 1 if SE == 1 & high_educ == 1 & migtype == 1
bys				year geolev2: egen total_high_educ_SE_mig_n = total(high_educ_SE_mig_n*perwt)
bys				year geolev2: gen prop_high_educ_SE_mig_n = total_high_educ_SE_mig_n/pop_cst
rename 			prop_high_educ_SE_mig_n high_educ_SE_per_mig_n
bys				year geolev2: egen prop_high_educ_SE_mig_n = max(high_educ_SE_per_mig_n)
gen				low_educ_SE_mig_n = 1 if SE == 1 & high_educ == 0 & migtype == 1
bys				year geolev2: egen total_low_educ_SE_mig_n = total(low_educ_SE_mig_n*perwt)
bys				year geolev2: gen prop_low_educ_SE_mig_n = total_low_educ_SE_mig_n/pop_cst
rename 			prop_low_educ_SE_mig_n low_educ_SE_per_mig_n
bys				year geolev2: egen prop_low_educ_SE_mig_n = max(low_educ_SE_per_mig_n)
keep			prop* geolev2 geolev1 year LFP emp wage SE emp unemp unpaid high_educ native migtype oldmig pop_cst ///
				total_native  perwt edattain
				
gen				low_educ_unemp_native = 1 if unemp == 1 & high_educ == 0 & native == 1
bys				year geolev2: egen total_low_educ_unemp_native = total(low_educ_unemp_native*perwt)
bys				year geolev2: gen prop_low_educ_unemp_native = total_low_educ_unemp_native/pop_cst
rename 			prop_low_educ_unemp_native low_educ_unemp_per_native
bys				year geolev2: egen prop_low_educ_unemp_native = max(low_educ_unemp_per_native)
gen				low_educ_unemp_mig_o = 1 if unemp == 1 & high_educ == 0 & oldmig == 1
bys				year geolev2: egen total_low_educ_unemp_mig_o = total(low_educ_unemp_mig_o*perwt)
bys				year geolev2: gen prop_low_educ_unemp_mig_o = total_low_educ_unemp_mig_o/pop_cst
rename 			prop_low_educ_unemp_mig_o low_educ_unemp_per_mig_o
bys				year geolev2: egen prop_low_educ_unemp_mig_o = max(low_educ_unemp_per_mig_o)
gen				high_educ_unemp_native = 1 if unemp == 1 & high_educ == 1 & native == 1
bys				year geolev2: egen total_high_educ_unemp_native = total(high_educ_unemp_native*perwt)
bys				year geolev2: gen prop_high_educ_unemp_native = total_high_educ_unemp_native/pop_cst
rename 			prop_high_educ_unemp_native high_educ_unemp_per_native
bys				year geolev2: egen prop_high_educ_unemp_native = max(high_educ_unemp_per_native)
gen				high_educ_unemp_mig_o = 1 if unemp == 1 & high_educ == 1 & oldmig == 1
bys				year geolev2: egen total_high_educ_unemp_mig_o = total(high_educ_unemp_mig_o*perwt)
bys				year geolev2: gen prop_high_educ_unemp_mig_o = total_high_educ_unemp_mig_o/pop_cst
rename 			prop_high_educ_unemp_mig_o high_educ_unemp_per_mig_o
bys				year geolev2: egen prop_high_educ_unemp_mig_o = max(high_educ_unemp_per_mig_o)
gen				high_educ_unemp_mig_n = 1 if unemp == 1 & high_educ == 1 & migtype == 1
bys				year geolev2: egen total_high_educ_unemp_mig_n = total(high_educ_unemp_mig_n*perwt)
bys				year geolev2: gen prop_high_educ_unemp_mig_n = total_high_educ_unemp_mig_n/pop_cst
rename 			prop_high_educ_unemp_mig_n high_educ_unemp_per_mig_n
bys				year geolev2: egen prop_high_educ_unemp_mig_n = max(high_educ_unemp_per_mig_n)
gen				low_educ_unemp_mig_n = 1 if unemp == 1 & high_educ == 0 & migtype == 1
bys				year geolev2: egen total_low_educ_unemp_mig_n = total(low_educ_unemp_mig_n*perwt)
bys				year geolev2: gen prop_low_educ_unemp_mig_n = total_low_educ_unemp_mig_n/pop_cst
rename 			prop_low_educ_unemp_mig_n low_educ_unemp_per_mig_n
bys				year geolev2: egen prop_low_educ_unemp_mig_n = max(low_educ_unemp_per_mig_n)
bys				year geolev2: egen total_native_HE = sum(native*perwt) if high_educ == 1
rename			total_native_HE HE_total_native
bys				year geolev2: egen total_native_HE = max(HE_total_native)
bys				year geolev2: egen total_native_LE = sum(native*perwt) if high_educ == 0
rename			total_native_LE LE_total_native
bys				year geolev2: egen total_native_LE = max(LE_total_native)
keep			prop_* high_educ LFP emp wage SE unemp unpaid pop_cst native ///
				total_native  migtype edattain total_native_LE total_native_HE ///
				geolev2 geolev1 year perwt oldmig high_educ 

forval 			i = 1(1)4 {
gen				educ`i'_mig_n = 1 if edattain == `i' & migtype == 1
bys				year geolev2: egen total_educ`i'_mig_n = total(educ`i'_mig_n*perwt)
bys				year geolev2: gen prop_educ`i'_mig_n = total_educ`i'_mig_n/pop_cst
rename 			prop_educ`i'_mig_n educ`i'_per_mig_n
bys				year geolev2: egen prop_educ`i'_mig_n = max(educ`i'_per_mig_n)
}
keep			prop_* high_educ LFP emp wage SE unemp unpaid pop_cst native ///
				total_native  migtype edattain total_native_LE total_native_HE ///
				geolev2 geolev1 year perwt oldmig high_educ prop_educ*_mig_n				
									
duplicates		drop geolev2 year, force
tempfile		a_`t'_work
save			`a_`t'_work', replace
}

use				`a_1976_work', clear
foreach			t in 1980 1985 1990 1995 2000 2005 2010{
append			using `a_`t'_work'
}
egen 			prop1_sum = rowtotal(prop_educ1_mig_n prop_educ2_mig_n prop_educ3_mig_n prop_educ4_mig_n)
save			"Data/EDCC_Rev1/DV with migrant educ", replace

/*******************************************************************************
Education of Migrants: Reduced form regressions
********************************************************************************/
use			"Data/EDCC_Rev1/DV with migrant educ", clear
merge 		1:1 year geolev2 using "Data/Merged", ///
			keepusing(pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y ///
			av_high_educ_ct prop_male_ct av_age_ct)
keep 		if _merge==3
drop 		_merge

sum 		prop1_sum
gen 		prop1 = prop_educ1_mig_n/prop1_sum
gen 		prop2 = prop_educ2_mig_n/prop1_sum
gen 		prop3 = prop_educ3_mig_n/prop1_sum
gen 		prop4 = prop_educ4_mig_n/prop1_sum


reghdfe 		prop1 pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y /// 
				av_high_educ_ct prop_male_ct av_age_ct, /// 
				a(i.year#i.geolev1 i.geolev2) cl(geolev1)
local 			clusters = `e(N_clust)'
su				`e(depvar)' if e(sample) == 1, d				
local 			meandv = r(mean)
outreg2			using "Tables/DV with migrant educ.xlsx", excel tex replace ///
				dec(3) ctitle(5Y Alpha) /// 
				keep (pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y) /// 
				nocons nonotes addtext("District FE", YES, "Province-Year FE", YES, ///
				"# of districts", `clusters', "Mean of DV", `: di %9.2f `meandv'')	

reghdfe 		prop2 pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y /// 
				av_high_educ_ct prop_male_ct av_age_ct, /// 
				a(i.year#i.geolev1 i.geolev2) cl(geolev1)
local 			clusters = `e(N_clust)'
su				`e(depvar)' if e(sample) == 1, d				
local 			meandv = r(mean)
outreg2			using "Tables/DV with migrant educ.xlsx", excel tex append ///
				dec(3) ctitle(5Y Alpha) /// 
				keep (pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y) /// 
				nocons nonotes addtext("District FE", YES, "Province-Year FE", YES, ///
				"# of districts", `clusters', "Mean of DV", `: di %9.2f `meandv'')	
				
reghdfe 		prop3 pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y /// 
				av_high_educ_ct prop_male_ct av_age_ct, /// 
				a(i.year#i.geolev1 i.geolev2) cl(geolev1)
local 			clusters = `e(N_clust)'
su				`e(depvar)' if e(sample) == 1, d				
local 			meandv = r(mean)
outreg2			using "Tables/DV with migrant educ.xlsx", excel tex append ///
				dec(3) ctitle(5Y Alpha) /// 
				keep (pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y) /// 
				nocons nonotes addtext("District FE", YES, "Province-Year FE", YES, ///
				"# of districts", `clusters', "Mean of DV", `: di %9.2f `meandv'')	
				
reghdfe 		prop4 pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y /// 
				av_high_educ_ct prop_male_ct av_age_ct, /// 
				a(i.year#i.geolev1 i.geolev2) cl(geolev1)
local 			clusters = `e(N_clust)'
su				`e(depvar)' if e(sample) == 1, d				
local 			meandv = r(mean)
outreg2			using "Tables/DV with migrant educ.xlsx", excel tex append ///
				dec(3) ctitle(5Y Alpha) /// 
				keep (pred_per_immig_cst_10Y_5Y pred_per_immig_cst_Jaeger_10Y) /// 
				nocons nonotes addtext("District FE", YES, "Province-Year FE", YES, ///
				"# of districts", `clusters', "Mean of DV", `: di %9.2f `meandv'')	

















































